package com.example.springbootweb01.service.impl;

import com.example.springbootweb01.dao.UserDao;
import com.example.springbootweb01.pojo.User;
import com.example.springbootweb01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
//@Component
public class UserServiceimpl implements UserService {
    @Autowired
    private UserDao userDao;

    //2.解析用户信息，封装User对象
    @Override
    public List<User> findAll() {
        List<String> lines = userDao.findAll();
        List<User> usersList = lines.stream().map(line -> {
            String[] parts = line.split(",");
            Integer id = Integer.parseInt(parts[0]);
            String username = parts[1];
            String password = parts[2];
            String name = parts[3];
            Integer age = Integer.parseInt(parts[4]);
            LocalDateTime updataTime = LocalDateTime.parse((parts[5]), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(id, username, password, name, age, updataTime);
        }).toList();
        return usersList;
    }

}
